package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.database.Cursor;

import androidx.annotation.NonNull;

import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.DataItemLatest;
import com.xuexiang.yoyo.entity.DataItemResult;

import java.util.ArrayList;
import java.util.List;

public class DataItemLatestMapper {
    public static final String TABLE = "data_item_latest";
    public static final String CREATE_TABLE_SQL="CREATE TABLE "+TABLE+"(id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "device_code TEXT," +
            "ln TEXT," +
            "attribute_index INTEGER," +
            "data_item_name TEXT," +
            "description TEXT ," +
            "value TEXT," +
            "gxdlms_class_name TEXT," +
            "username TEXT," +
            "create_time TEXT,"+
            "source TEXT," +
            "unit TEXT" +
            ");";


    public static List<DataItemLatest> queryByLimit(int pageNo, int pageSize){
        Cursor cursor =  HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where username='" + HesAppStatic.getUsername() + "' limit " + pageNo + "," + pageSize);
        List<DataItemLatest> resultList = getDataItemLatests(cursor);
        cursor.close();
        return resultList;
    }

    public static int count(){
        int result = 0;
        Cursor cursor =  HesAppStatic.getmDataBaseOpenHelper().customQuery("select count(*) from " + TABLE + " where username='" + HesAppStatic.getUsername() + "'");
        if(cursor.moveToNext()) result = cursor.getInt(0);
        cursor.close();
        return result;
    }


    public static void delete(String device_code){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() + "' and device_code= '"+device_code+"'",null);
    }


    public static void delete(){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() +"'",null);
    }
    public static void insertDlmsCosem(DataItemResult singleResultDataItem){
        DataItemLatest dataItemLatest = new DataItemLatest();
        dataItemLatest.setDeviceCode(singleResultDataItem.getInsertDeviceCode());
        dataItemLatest.setLn(singleResultDataItem.getLn());
        dataItemLatest.setAttributeIndex(singleResultDataItem.getAttributeIndex());
        dataItemLatest.setDataItemName(singleResultDataItem.getName());
        dataItemLatest.setDescription(singleResultDataItem.getDescription());
        dataItemLatest.setValue(singleResultDataItem.getValue());
        dataItemLatest.setGxdlmsClassName(singleResultDataItem.getGxdlmsClassName());
        dataItemLatest.setUsername(singleResultDataItem.getUsername());
        dataItemLatest.setSource("dlms-cosem");
        dataItemLatest.setUnit(singleResultDataItem.getUnit());
        insert(dataItemLatest);
    }

    public static void insert(DataItemLatest item){
        ContentValues contentValues = new ContentValues();
        contentValues.put("description", item.getDescription());
        contentValues.put("value", item.getValue());
        contentValues.put("data_item_name", item.getDataItemName());
        contentValues.put("ln", item.getLn());
        contentValues.put("attribute_index", item.getAttributeIndex());
        contentValues.put("gxdlms_class_name", item.getGxdlmsClassName());
        contentValues.put("device_code", item.getDeviceCode());
        contentValues.put("source", item.getSource());
        contentValues.put("username", HesAppStatic.getUsername());
        contentValues.put("create_time", System.currentTimeMillis());
        contentValues.put("unit", item.getUnit());
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE,contentValues);
    }
    public static List<DataItemLatest> queryByDeviceCode(String deviceCode){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where device_code='" + deviceCode + "'"+
                " and username='" + HesAppStatic.getUsername() + "'");
        List<DataItemLatest> resultList = getDataItemLatests(cursor);
        cursor.close();
        return resultList;
    }


    public static List<DataItemLatest> queryByUsername(){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where  username='" + HesAppStatic.getUsername() + "'");
        List<DataItemLatest> resultList = getDataItemLatests(cursor);
        cursor.close();
        return resultList;
    }

    @NonNull
    private static List<DataItemLatest> getDataItemLatests(Cursor cursor) {
        List<DataItemLatest> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            DataItemLatest item = new DataItemLatest();
            item.setId(cursor.getInt(0));
            item.setDeviceCode(cursor.getString(1));
            item.setLn(cursor.getString(2));
            item.setAttributeIndex(cursor.getInt(3));
            item.setDataItemName(cursor.getString(4));
            item.setDescription(cursor.getString(5));
            item.setValue(cursor.getString(6));
            item.setGxdlmsClassName(cursor.getString(7));
            item.setUsername(cursor.getString(8));
            item.setCreateTime(cursor.getLong(9));
            item.setSource(cursor.getString(10));
            item.setUnit(cursor.getString(11));
            resultList.add(item);

        }
        return resultList;
    }

}
